package com.zhaojun.cloud.common.util;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Objects;


public class ObjectUtil extends ObjectUtils {

	public static final short INVALID_NUMBER_VALUE = 0;

	private static Logger logger = LoggerFactory.getLogger(ObjectUtil.class);

	public static <T extends Object> byte[] convertToByteArray(T t) {
		byte[] bytes = null;
		if (t != null) {
			ByteArrayOutputStream byteArrayOutputStream = null;
			ObjectOutputStream objectOutputStream = null;
			try {
				byteArrayOutputStream = new ByteArrayOutputStream();
				objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
				objectOutputStream.writeObject(t);
				bytes = byteArrayOutputStream.toByteArray();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new RuntimeException(e);
			} finally {
				IOUtils.closeQuietly(objectOutputStream);
				IOUtils.closeQuietly(byteArrayOutputStream);
			}
		}
		return bytes;
	}

	public static String asTrimString(Object obj) {
		if (obj == null) {
			return null;
		}
		return Objects.toString(obj).trim();
	}

	public static short asPrimitiveShort(Object value) {
		String strValue = asTrimString(value);
		if (StringUtils.isNotBlank(strValue) && NumberUtils.isCreatable(strValue)) {
			return Short.parseShort(strValue);
		}
		return INVALID_NUMBER_VALUE;
	}

	public static int asPrimitiveInt(Object value) {
		String strValue = asTrimString(value);
		if (StringUtils.isNotBlank(strValue) && NumberUtils.isCreatable(strValue)) {
			return Integer.parseInt(strValue);
		}
		return INVALID_NUMBER_VALUE;
	}

	public static long asPrimitiveLong(Object value) {
		String strValue = asTrimString(value);
		if (StringUtils.isNotBlank(strValue) && NumberUtils.isCreatable(strValue)) {
			return Long.parseLong(strValue);
		}
		return INVALID_NUMBER_VALUE;
	}

	public static Short asShort(Object value) {
		String strValue = asTrimString(value);
		if (StringUtils.isNotBlank(strValue) && NumberUtils.isCreatable(strValue)) {
			return Short.valueOf(strValue);
		}
		return null;
	}

	public static Integer asInteger(Object value) {
		String strValue = asTrimString(value);
		if (StringUtils.isNotBlank(strValue) && NumberUtils.isCreatable(strValue)) {
			return Integer.valueOf(strValue);
		}
		return null;
	}

	public static Long asLong(Object value) {
		String strValue = asTrimString(value);
		if (StringUtils.isNotBlank(strValue) && NumberUtils.isCreatable(strValue)) {
			return Long.valueOf(strValue);
		}
		return null;
	}

	public static List<?> asList(Object object) {
		if (object != null && object instanceof List<?>) {
			return (List<?>) object;
		}
		return null;
	}

	public static double asPrimitiveDouble(Object value) {
		String strValue = asTrimString(value);
		if (StringUtils.isNotBlank(strValue) && NumberUtils.isCreatable(strValue)) {
			return Double.parseDouble(strValue);
		}
		return INVALID_NUMBER_VALUE;
	}

	public static String asString(Object object) {
		if (null != object) {
			return StringUtils.trim(String.valueOf(object));
		}
		return null;
	}

	public static Object[] asArray(List<?> list) {
		if (Detect.notEmpty(list)) {
			return list.toArray();
		}
		return null;
	}
}
